///*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//
//package controller;
//
//import common.Constant;
//import common.UserBindListener;
//import entity.AccountLocal;
//import java.io.IOException;
//import javax.servlet.ServletContext;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import session.AccountBeanLocal;
//import util.CryptoUtil;
//import util.EJBUtil;
//import util.WebUtil;
//
///**
// *
// * @author Giang Nguyen
// */
//public class LoginController extends HttpServlet {
//
//    /**
//     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
//     * @param request servlet request
//     * @param response servlet response
//     * @throws ServletException if a servlet-specific error occurs
//     * @throws IOException if an I/O error occurs
//     */
//    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
//    throws ServletException, IOException {
//        String nextPage = null;
//        String target = request.getParameter("hiddenTarget"); // return url
//        ServletContext context = getServletContext();
//        HttpSession session = request.getSession();
//        try {
//            String username = request.getParameter("txtUsername");
//            String password = request.getParameter("txtPassword");
//            AccountBeanLocal accountBeanLocal = EJBUtil.getAccountBeanLocal();
//            AccountLocal account = accountBeanLocal.findUserByUsername(username);
//            // Check user is approved or not
//            if (account.getMembership().getApproved().shortValue() == 1) { // Approved
//                String encryptedPassword = CryptoUtil.encryptPasswordToHex(password, account.getSalt());
//                if (encryptedPassword.equals(account.getPassword())) { // valid password
//                    session.setAttribute(Constant.USER_SES, new UserBindListener(context, account));
//                    if (target != null)
//                        nextPage = target;
//                    else
//                        nextPage = Constant.CONTEXT_PATH;
//                } else { // invalid password.
//                    if (target != null)
//                        nextPage = Constant.CONTEXT_PATH + "/login?err=Invalid username or password. Please try again!&target=" + target;
//                    else
//                        nextPage = Constant.CONTEXT_PATH + "/login?err=Invalid username or password. Please try again!";
//                }
//            } else { // Unapproved
//                nextPage = Constant.CONTEXT_PATH + "/login?err=Your account is inactive state! Please activates your account or contacts to webmaster!";
//            }
//        } catch (Exception ex) {
////            WebUtil.getLogger(LoginController.class).debug("Login failse: " + ex.getMessage());
//            if (target != null)
//                nextPage = Constant.CONTEXT_PATH + "/login?err=Invalid username or password. Please try again!&target=" + target;
//            else
//                nextPage = Constant.CONTEXT_PATH + "/login?err=Invalid username or password. Please try again!";
//        } finally {
//            response.sendRedirect(nextPage);
//        }
//    }
//
//    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
//    /**
//     * Handles the HTTP <code>GET</code> method.
//     * @param request servlet request
//     * @param response servlet response
//     * @throws ServletException if a servlet-specific error occurs
//     * @throws IOException if an I/O error occurs
//     */
//    protected void doGet(HttpServletRequest request, HttpServletResponse response)
//    throws ServletException, IOException {
//        processRequest(request, response);
//    }
//
//    /**
//     * Handles the HTTP <code>POST</code> method.
//     * @param request servlet request
//     * @param response servlet response
//     * @throws ServletException if a servlet-specific error occurs
//     * @throws IOException if an I/O error occurs
//     */
//    protected void doPost(HttpServletRequest request, HttpServletResponse response)
//    throws ServletException, IOException {
//        processRequest(request, response);
//    }
//
//    /**
//     * Returns a short description of the servlet.
//     * @return a String containing servlet description
//     */
//    public String getServletInfo() {
//        return "Short description";
//    }// </editor-fold>
//
//}
